require("dotenv").config();
const Koa = require("koa");
const static = require("koa-static");
const userRouter = require("../routers/user.route");
const path = require("path");
const cors = require("@koa/cors");
const uploadMiddleware = require("../middleware/upload"); // 引入自定义的上传中间件
const fs = require("fs");

const app = new Koa();

const isDevelopment = process.env.NODE_ENV === "development";

// 使用 CORS 仅在开发阶段
if (isDevelopment) {
    app.use(cors());
}

const staticPath = path.join(__dirname, '../../selected_pics');
app.use(static(staticPath));

app.use(async (ctx, next) => {
    await next();
});

app.use(uploadMiddleware); // 解析请求体
// 使用用户路由
app.use(userRouter.routes());
app.use(userRouter.allowedMethods());

module.exports = app;
